home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / lib / cmdtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  8.1 KB  |  275 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: cmdtest.c - test program for the CmdLine library
  3. //
  4. // ^DESCRIPTION:
  5. //    This program tests as many features of command-line as possible.
  6. //
  7. // ^HISTORY:
  8. //    03/18/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  9. //
  10. //    03/01/93    Brad Appleton    <brad@ssd.csd.harris.com>
  11. //    - Attached a description to the command.
  12. //-^^---------------------------------------------------------------------
  13.  
  14. #include <stdlib.h>
  15. #include <iostream.h>
  16. #include <ctype.h>
  17. #include <cmdargs.h>
  18.  
  19. //---------------------------------------------------------------- CmdArgModCmd
  20.  
  21.    // CmdArgModCmd is a special argument that we use for testing.
  22.    // The argument actually modifies the flags of the associated
  23.    // command before it has finished parsing the arguments, hence
  24.    // the new flags take effect for all remaining arguments.
  25.    //
  26.    // This argument takes a value (which is optional). If no value
  27.    // is given, then the flags are unset, otherwise the value is
  28.    // a list of characters, each of which corresponds to a CmdFlag
  29.    // to turn on.
  30.    //
  31. class  CmdArgModCmd : public CmdArg {
  32. public:
  33.    CmdArgModCmd(char         optchar,
  34.                 const char * keyword,
  35.                 const char * value,
  36.                 const char * description,
  37.                 unsigned     syntax_flags =CmdArg::isOPTVALOPT);
  38.  
  39.    virtual
  40.    ~CmdArgModCmd(void);
  41.  
  42.    virtual  int
  43.    operator()(const char * & arg, CmdLine & cmd);
  44. } ;
  45.  
  46. CmdArgModCmd::CmdArgModCmd(char         optchar,
  47.                            const char * keyword,
  48.                            const char * value,
  49.                            const char * description,
  50.                            unsigned     syntax_flags)
  51.    : CmdArg(optchar, keyword, value, description, syntax_flags) {}
  52.  
  53. CmdArgModCmd::~CmdArgModCmd(void) {}
  54.  
  55. int CmdArgModCmd::operator()(const char * & arg, CmdLine & cmd)
  56. {
  57.    unsigned  new_flags = 0;
  58.    for (const char * p = arg ; *p ; p++) {
  59.       char ch = *p;
  60.       if (isupper(ch))  ch = tolower(ch);
  61.       switch (ch) {
  62.          case 'c' : new_flags |= CmdLine::ANY_CASE_OPTS;  break;
  63.  
  64.          case 'p' : new_flags |= CmdLine::PROMPT_USER;    break;
  65.  
  66.          case 'n' : new_flags |= CmdLine::NO_ABORT;       break;
  67.  
  68.          case 'f' : new_flags |= CmdLine::OPTS_FIRST;     break;
  69.  
  70.          case 'o' : new_flags |= CmdLine::OPTS_ONLY;      break;
  71.  
  72.          case 'k' : new_flags |= CmdLine::KWDS_ONLY;      break;
  73.  
  74.          case 't' : new_flags |= CmdLine::TEMP;           break;
  75.  
  76.          case 'q' : new_flags |= CmdLine::QUIET;          break;
  77.  
  78.          case 'g' : new_flags |= CmdLine::NO_GUESSING;    break;
  79.  
  80.          case '+' : new_flags |= CmdLine::ALLOW_PLUS;     break;
  81.  
  82.          default  : break;
  83.       } //switch
  84.    } //for
  85.    cmd.flags(new_flags);
  86.    arg = NULL;
  87.    return  0;
  88. }
  89.  
  90.  
  91. //------------------------------------------------------ Command Line Arguments
  92.  
  93. static CmdArgModCmd    fflag('f', "flags", "[cpnfoktqg]",
  94.    "Use this argument to change the behavior of \
  95. parsing for all remaining arguments.  If no \
  96. value is given   then the command-flags are \
  97. cleared.  Otherwise each letter specifies a flag \
  98. to set:\n\
  99.    'c' = any-Case-opts\n\
  100.    'p' = Prompt-user\n\
  101.    'n' = No-abort\n\
  102.    'f' = options-First\n\
  103.    'o' = Opts-only\n\
  104.    'k' = Keywords-only\n\
  105.    't' = Temporary-args\n\
  106.    'q' = Quiet!\n\
  107.    'g' = no-Guessing\n\
  108.    '+' = allow-plus\n\
  109. This-is-a-very-long-line-containing-no-whitespace-\
  110. characters-and-I-just-want-to-see-if-it-gets-\
  111. formatted-appropriately!"
  112.    );
  113.  
  114. static CmdArgStr       str('s', "str", "[string]", "string to parse");
  115. static CmdArgInt       debug ('D', "Debug", "[level]", "turn on debugging",
  116.                               CmdArg::isVALSTICKY);
  117. static CmdArgBool      infile('p', "parse", "parse from cin");
  118.  
  119. static CmdArgSet       xflag('x', "x", ";turn on X-rated mode");
  120. static CmdArgClearRef  nxflag(xflag, 'n', "nx", ";turn off X-rated mode");
  121. static CmdArgInt       count('c', "count", "number", "number of copies");
  122. static CmdArgChar      delim('d', "delimiter", "char", "delimiter character");
  123. static CmdArgChar      ext('e', "ext", "[char]", "extension to use",
  124.                                                  CmdArg::isVALSTICKY);
  125. static CmdArgChar      code('C', "Code", "char", "code to use",
  126.                                                  CmdArg::isVALSTICKY);
  127. static CmdArgStr       why('y', "why", "[reason]", "specify the reason why",
  128.                                                    CmdArg::isVALSEP);
  129. static CmdArgStr       who('w', "who", "logname", "the user responsible",
  130.                                                   CmdArg::isVALSEP);
  131. static CmdArgIntList   ints('i', "int", "number ...", "list of ints");
  132. static CmdArgStrList   grps('g', "groups", "newsgroup", "list of newsgroups");
  133. static CmdArgDummy     dummy("--", "denote end of options");
  134. static CmdArgStr       name('n', "name", "name", "name of document",
  135.                                                  CmdArg::isPOS);
  136. static CmdArgStrList   files("[files ...]", "files to process");
  137.  
  138. //------------------------------------------------------------------ print_args
  139.  
  140. static void
  141. print_args(void) {
  142.    cout << "xflag=" << (xflag ? "ON" : "OFF") << endl ;
  143.    cout << "count=" << count << endl ;
  144.  
  145.    unsigned  sflags = str.flags();
  146.    if ((sflags & CmdArg::GIVEN) && (! (sflags & CmdArg::VALGIVEN))) {
  147.       cout << "No string given on command-line!" << endl ;
  148.    } else {
  149.       cout << "str=\"" << str << "\"" << endl ;
  150.    }
  151.    cout << "delim='" << delim << "'" << endl ;
  152.    cout << "ext='" << ext << "'" << endl ;
  153.    cout << "code='" << code << "'" << endl ;
  154.    cout << "why=\"" << why << "\"" << endl ;
  155.    cout << "who=\"" << who << "\"" << endl ;
  156.  
  157.    unsigned  nints = ints.count();
  158.    for (int i = 0; i < nints ; i++) {
  159.       cout << "int[" << i << "]=" << ints[i] << endl ;
  160.    }
  161.  
  162.    unsigned  ngrps = grps.count();
  163.    for (i = 0; i < ngrps ; i++) {
  164.       cout << "groups[" << i << "]=\"" << grps[i] << "\"" << endl ;
  165.    }
  166.  
  167.    cout << "name=\"" << name << "\"" << endl ;
  168.  
  169.    unsigned  nfiles = files.count();
  170.    for (i = 0; i < nfiles ; i++) {
  171.       cout << "files[" << i << "]=\"" << files[i] << "\"" << endl ;
  172.    }
  173. }
  174.  
  175. //------------------------------------------------------------------------ dump
  176.  
  177. static void
  178. dump(CmdLine & cmd)
  179. {
  180.    if (debug) {
  181.       cmd.dump(cout);
  182.       if (debug > 1) cmd.dump_args(cout);
  183.    }
  184. }
  185.  
  186. //------------------------------------------------------------------------ main
  187.  
  188. int
  189. main(int argc, char * argv[]) {
  190.    CmdLine  cmd(*argv,
  191.                 & fflag,
  192.                 & str,
  193.                 & infile,
  194.                 & debug,
  195.                 & xflag,
  196.                 & nxflag,
  197.                 & count,
  198.                 & delim,
  199.                 & ext,
  200.                 & code,
  201.                 & why,
  202.                 & who,
  203.                 & ints,
  204.                 & grps,
  205.                 & dummy,
  206.                 & name,
  207.                 & files,
  208.                 NULL);
  209.  
  210.    CmdArgvIter  argv_iter(--argc, ++argv);
  211.  
  212.    cmd.description(
  213. "This program is intended to statically and dynamically test \
  214. the CmdLine(3C++) class library."
  215.    );
  216.  
  217.    cout << "Test of " << CmdLine::ident() << endl ;
  218.  
  219.    xflag = 0;
  220.    count = 1;
  221.    str = NULL;
  222.    delim = '\t';
  223.    name = NULL;
  224.  
  225.    cout << "Parsing the command-line ..." << endl ;
  226.    unsigned status = cmd.parse(argv_iter);
  227.    if (status)  cmd.error() << "parsing errors occurred!" << endl ;
  228.  
  229.    print_args();
  230.  
  231.    unsigned dbg_flags = debug.flags();
  232.    if ((dbg_flags & CmdArg::GIVEN) && (! (dbg_flags & CmdArg::VALGIVEN))) {
  233.       debug = 1;
  234.    }
  235.  
  236.    dump(cmd);
  237.  
  238.    int  parse_cin = infile;
  239.  
  240.    // Parse arguments from a string
  241.    if (! str.isNULL()) {
  242.       CmdStrTokIter  tok_iter(str);
  243.  
  244.       xflag = 0;
  245.       count = 1;
  246.       str = NULL;
  247.       delim = '\t';
  248.       name = NULL;
  249.  
  250.       cout << "\n\nParsing the string ..." << endl ;
  251.       status = cmd.parse(tok_iter);
  252.       print_args();
  253.       dump(cmd);
  254.    }
  255.  
  256.  
  257.    // Parse arguments from a file
  258.    if (parse_cin) {
  259.       xflag = 0;
  260.       count = 1;
  261.       str = NULL;
  262.       delim = '\t';
  263.       name = NULL;
  264.  
  265.       CmdIstreamIter  file_iter(cin);
  266.       cout << "\n\nParsing from cin ..." << endl ;
  267.       status = cmd.parse(file_iter);
  268.       print_args();
  269.       dump(cmd);
  270.    }
  271.  
  272.    return  0;
  273. }
  274.  
  275.